package com.caothai.demo_tracnghiem;

import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class Demo_tracnghiemActivity extends Activity {
	TextView textview_tieude, textview_cauhoi, textview_traloi1, textview_traloi2, textview_traloi3, textview_traloi4,textview_ketqua;
	TextView textview_theloai, textview_mucdo;
	RadioButton radio_A,radio_B,radio_C,radio_D;
	Button button_xacnhan;

	ArrayList<Cauhoi> danhsach_cauhoi= new ArrayList<Cauhoi>();
	int socauhoi=3;
	int cauhoi_hienhanh=0;
	int diemso=0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tracnghiem);
        // đi tìm
        textview_tieude=(TextView) findViewById(R.id.textview_tieude);
        textview_cauhoi = (TextView) findViewById(R.id.textview_cauhoi);
        textview_traloi1= (TextView) findViewById(R.id.textview_traloi1);
        textview_traloi2= (TextView) findViewById(R.id.textview_traloi2);
        textview_traloi3= (TextView) findViewById(R.id.textview_traloi3);
        textview_traloi4= (TextView) findViewById(R.id.textview_traloi4);
        textview_theloai= (TextView) findViewById(R.id.textview_theloai);
        textview_mucdo= (TextView) findViewById(R.id.textview_mucdo);
        textview_ketqua= (TextView) findViewById(R.id.textview_ketqua);
        radio_A= (RadioButton) findViewById(R.id.radio_A);
        radio_B= (RadioButton) findViewById(R.id.radio_B);
        radio_C= (RadioButton) findViewById(R.id.radio_C);
        radio_D= (RadioButton) findViewById(R.id.radio_D);
        button_xacnhan=(Button) findViewById(R.id.button_xacnhan);
        nap_cauhoi_vaoCSDL();
        tao_de_thi_trac_nghiem();
        hienthi_cauhoi_i(0);
        
        button_xacnhan.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				// xem xet cau tra loi dung hay sai, cap nhat diem so
				
			}
		});
        
    }
    
    void tao_de_thi_trac_nghiem(){
    	Quanly_cauhoi db= new Quanly_cauhoi(this);
    	danhsach_cauhoi= db.get_ngaunhien_n_Cauhoi(socauhoi);
    }
    void hienthi_cauhoi_i(int i){
    	String noidung_cauhoi=danhsach_cauhoi.get(i).getNoidung_cauhoi().replace("\n", "");
    	String noidung_traloi1=danhsach_cauhoi.get(i).getNoidung_traloi1().replace("\n", "");
    	String noidung_traloi2=danhsach_cauhoi.get(i).getNoidung_traloi2().replace("\n", "");
    	String noidung_traloi3=danhsach_cauhoi.get(i).getNoidung_traloi3().replace("\n", "");
    	String noidung_traloi4=danhsach_cauhoi.get(i).getNoidung_traloi4().replace("\n", "");
    	String theloai=danhsach_cauhoi.get(i).getTheloai().replace("\n", "");
    	String mucdo=danhsach_cauhoi.get(i).getMucdo().replace("\n", "");

    	textview_tieude.setText("Trắc nghiệm - Câu "+(i+1));
    	
    	textview_cauhoi.setText(noidung_cauhoi);
    	textview_traloi1.setText("a) "+noidung_traloi1);
    	textview_traloi2.setText("b) "+noidung_traloi2);
    	textview_traloi3.setText("c) "+noidung_traloi3);
    	textview_traloi4.setText("d) "+noidung_traloi4);

    	textview_theloai.setText("Thể loại: "+ theloai);
    	textview_mucdo.setText("Mức độ: "+ mucdo);
    }
    // doc file text ngan hang cau hoi ghi vao CSDL
    void nap_cauhoi_vaoCSDL(){
    	try{ 
    		Quanly_cauhoi db= new Quanly_cauhoi(this);
    		
    		InputStream input = getAssets().open("nganhang_cauhoi.txt");         
    		int size = input.available(); 
    		byte[] buffer = new byte[size]; 
    		input.read(buffer); 
    		input.close(); 
    		String text = new String(buffer); 
    		text=text.replace("\r\n", ""); // loai bỏ dấu xuống hàng về đầu dòng 
    		// phan tach
    		String[] ds_cauhoi=text.split("###");
    		for (int i=1;i<ds_cauhoi.length;i++){
    			// phan tach thanh phan 
    			String[] temp = ds_cauhoi[i].split("#");
    			
    			String noidung_cauhoi= temp[0];
    			String noidung_traloi1= temp[1];
    			String noidung_traloi2= temp[2];
    			String noidung_traloi3= temp[3];
    			String noidung_traloi4= temp[4];
    			String dapan= temp[5];
    			String theloai= temp[6];
    			String mucdo= temp[7];

    			Cauhoi cauhoi= new Cauhoi(noidung_cauhoi, noidung_traloi1, noidung_traloi2, noidung_traloi3, noidung_traloi4, dapan, theloai, mucdo);
    			db.them_Cauhoi(cauhoi);
    			
    		}
    		
 
    		} catch (Exception ex){ 
    		Toast.makeText(this, "Khong the doc file nganhang_cauhoi.txt",Toast.LENGTH_LONG); 
    		} 
    }
    
}